package com.helloworld.chat.service.impl;

import com.helloworld.chat.dao.FriendDao;
import com.helloworld.chat.dao.FriendRequestDao;
import com.helloworld.chat.entity.Friend;
import com.helloworld.chat.entity.FriendRequest;
import com.helloworld.chat.entity.User;
import com.helloworld.chat.service.FriendRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xdzy on 2017/6/30.
 */
@Service
@Transactional
public class FriendRequestServiceImpl implements FriendRequestService {
    @Autowired
    private FriendRequestDao friendRequestDao;
    @Autowired
    private FriendDao friendDao;

    public void setFriendDao(FriendDao friendDao) {
        this.friendDao = friendDao;
    }

    public void setFriendRequestDao(FriendRequestDao friendRequestDao) {
        this.friendRequestDao = friendRequestDao;
    }

    @Override
    public boolean dealRequest(int id, boolean accept, int groupId) {
        if(accept){
            FriendRequest request=friendRequestDao.getFriendRequest(id);
            User me=request.getReceiver();
            User sender=request.getSender();
            Friend friend1=new Friend();//代表请求者的好友
            friend1.setFu(me);
            friend1.setGid(request.getGroup().getGid());
            friend1.setUid(sender.getUid());
            friend1.setAlias(me.getNickName());
            Friend friend2=new Friend();//代表接受者的好友
            friend2.setFu(sender);
            friend2.setGid(groupId);
            friend2.setUid(me.getUid());
            friend2.setAlias(sender.getNickName());
            friendDao.addFriend(friend1);
            friendDao.addFriend(friend2);
            finishRequest(id);
        }else{
            finishRequest(id);
        }
        return true;
    }

    @Override
    public void finishRequest(int id) {
        friendRequestDao.deleteRequest(id);
    }

    @Override
    public int getNotFinishCount(int id) {
        return friendRequestDao.getRequestCount(id);
    }

    @Override
    public List<FriendRequest> getMyRequest(int id) {
        return friendRequestDao.getMyFriendRequestList(id);
    }

    @Override
    public List<FriendRequest> getReceiveRequest(int id) {
        return friendRequestDao.getReceiveFriendRequestList(id);
    }
}
